IUG 2026 - Sproc Talk - Outline

Introduction


What do we talk about when we talk about sprocs?


Polaris Stored Procedures


Sprocs - What do they do and how do they work?

Polaris.Polaris.Rpt_NewTitles

SET ANSI_NULLS ON - Evaluates both {expression} = NULL and {expression} <> NULL as False if the value of {expression} is NULL.

SET QUOTED_IDENTIFIER ON - This gets way into the weeds as to the history of SQL Server, T-SQL, and ANSI, but here's what it does. When you have QUOTED_IDENTIFIER set to ON then:

You'll see GO occasionally, indeed, you see it twice here. If you're wondering why, it's because it's used to send batches of commands one at a time. In this case, ANSI_NULLS must be set on their own and, after they are, QUOTED_IDENTIFIERS must be set on their own.

ALTER PROCEDURE Polaris.Rpt_NewTitles

Stored procedures can be created, altered, dropped, and executed. In this example, I chose to pull the code for the procedure in such a way that, if I ran the code, it would change the procedure rather than trying to create it again and I really don't want to drop it!

@dtImported datetime,
@OrganizationList as NVARCHAR(MAX)

Here we're defining our parameters that will be passed to the stored procedure. As you can see we've got a datetime parameter that looks like it has something to do with when bibliographic records were imported. And this OrganizationList? Well, that's literally going to be holding on to your libraries. We'll get to that in a sec.

AS
SET NOCOUNT ON

We'll come back to those comments, because I love them. Seriously, I do. But if you're wondering what SET NOCOUNT ON does, in true coding fashion, setting NOCOUNT to ON actually turns off a feature.

Look, I didn't design this crap.

Normally, you would get another result set after running your sproc and that result set would give you a count of the rows affected. Do you need that? Probably not, so you set NOCOUNT to ON and now you don't get that extra result set.

Now, beloved... the comments. Look at this. This sproc lists "all new bibliographic records added to the database after a given date." Hey, Beth Silliman wrote this back in 2001. Polaris was brand freakin' new in 2001. Since I'm pushing 50, this code was written almost literally half my life ago. This code is a quarter of a century old. And it was modified once, just over a year later by Jim Mieczkowski. At one time, he was the CIO of Polaris. Jim added unicode support. And we know that because they told us that. This is why I always tell anyone who'll listen, comment your code. You never know how long it'll last. That query you wrote to do that one thing, and you're sure that you'll come up with something better later on?

Don't count on it. People tend to have a way of finding better things to do that fixing something that's not actually all that broken. Who knows? 25 years later, someone might be looking at your code to figure out how it works. And that somebody... might be you. I am almost positive that I have reports and sprocs that I wrote some ten years ago that are still running in some libraries today. Do the future a favour, tell them about your code.

So let's take a look at all of this:

I call this the preamble. Almost every stored procedure you'll see in SQL Server will look something like this. You'll have your ANSI_NULLS set, your QUOTED_IDENTIFIER, you'll have the operator and the name of the procedure, parameters, and the NOCOUNT. Please, put some comments in there somewhere too. The majority of Polaris sprocs have these comments. They've helped me a lot over the years.

Now, I'm going to do something I don't normally recommend, and I'm going to hand wave most of the rest of this code. And the reason I'm going to do that is I'm talking about structure and not substance. So instead of going through what all this does, which I'm sure you'll be thrilled to learn that it pulls a list of bibliographic records added to the system since a given date, I'm just going to say that "your query goes here." However, there are a couple things here worth pointing out:

And...

See these guys? They're the part of the code that uses those parameters we talked about earlier. We'll get into that in just a second, but I didn't want you to miss these because yeah, they're pretty important!

Okay, cool cool. The rest of this stuff? It's the query. This is where your query goes, right? So like the year 2026 in general, we're gonna go straight to the bottom and look at these last two lines.

So, you don't need to use RETURN, but it's good form to do so. And now that I've told you that, I can't tell you the last time I did that myself. I mean, hell, I'm the guy up here wearing a punk rock t-shirt... what do you expect? The RETURN statement brings your stored procedure to an end. It returns to your regularly scheduled SQL Server, already in progress. Now, with that, you might find yourself thinking, well... if RETURN takes you out of the sproc, why is there a GO after it? Wouldn't it exit before that?

Yes, and here's the little secret: The SQL Server execution system? It never sees GO. And you know what that means?

That means it never collects $200.

No, going back to what I said before, the GO statement tells SQL Server to run code in batches, on their own. So what you're seeing here, this code simply tells SQL Server, run this batch. SQL Server gets the batch, but not the word GO.

Okay, great, let's run this and see what happens!

Running down a dream sproc

So this is the easy part, especially for this sproc. We use the EXEC command, and if we're proper thinking individuals we use the full name of the sproc. Now, keep in mind, we need to give that sproc a date and an OrganizationID. And I dunno how y'all do bibliographic records at your library, so I'm going to play it safe and use a 0, because, if you go back and look at the code, it specifically says:

In other words, if I pass a 0, that means we get all the bib records!

So, here's the line:

That should pull all the bibs imported into this system since March 1, 2026 and, yes, it does.

Now, I'm not going to tell you that all of the Polaris stored procedures are that simple, but this is the foundational level of how things happen in the backend databases.

So, yeah... cool. Go check out some sprocs and see what their code looks like! As a colleague of mine often says "Always be noobin'." Go find yourself something new!

Speaking of something new... let's write our own sproc!

Nothin's says lovin' like sprocs from the oven

NOTE TO SELF: For the example sproc, let's use something that provides a list of MaxItems and MaxReuqestItems for a given patron code at a given branch.

Procedure

  1. Log into the server

  2. Open SSMS and login

  3. Open up the databases → Polaris → expand Programmability then Stored Procedures

  4. Right-click Stored Procedures and select New Stored Procedure

  5. Take a few minutes to go over the template

  6. Delete the comments at the top

  7. ANSI NULLS and QUOTED IDENTIFIER are already set to go

  8. Update the comments in the middle

  9. Explain the CREATE PROCEDURE line and edit for the example

  10. Open up the saved sproc and explain

  11. Create it

  12. Run it with parameters 52,3